package com.diaohw.module.auth.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.diaohw.module.auth.service.LogoutService;
import com.diaohw.platform.common.constant.RequestConstant;
import com.diaohw.platform.common.obj.login.LoginUser;
import com.diaohw.platform.common.obj.thread.LoginThread;
import com.diaohw.platform.framework.authorization.OnlineManager;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class LogoutImpl implements LogoutService {

    @Resource
    private OnlineManager onlineManager;

    @Override
    public boolean logout() {
        String loginId = LoginThread.getLoginId();
        if (loginId == null) {
            String value = StpUtil.getTokenValue();
            LoginUser user = RequestConstant.parseToken(value);
            if (user == null) {
                return false;
            }
            loginId = user.getLoginId();
        }
        onlineManager.logout(loginId);
        return true;
    }

}
